package com.zhaolang.qogir.ui.manage;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.zhaolang.qogir.R;
import com.zhaolang.qogir.bean.ProductClassBean;
import com.zhaolang.qogir.domain.BaseCall;
import com.zhaolang.qogir.domain.ReqFactory;
import com.zhaolang.qogir.domain.WaresManage;
import com.zhaolang.qogir.ui.BaseActivity;
import com.zhaolang.qogir.ui.ContextState;
import com.zhaolang.qogir.ui.TitleView;
import com.zhaolang.qogir.ui.adapter.manage.MangeProductClassifyAdapter;
import com.zhaolang.qogir.util.Slog;

import java.util.ArrayList;
import java.util.List;

/**
 * 分类管理
 */
public class ActivityManageProductClassify extends BaseActivity implements View.OnClickListener {
    private ListView manageProductLv;
    private LinearLayout manageBtnClassAdd;
    private TitleView mTitle;
    private MangeProductClassifyAdapter mangeProductClassifyAdapter;
    private List<ProductClassBean> classList = new ArrayList<>();
    private WaresManage waresManage;
    private final static int BASE = 100;
    private final static int MSG_UPDATE_CLASS_LAYOUT = BASE + 1;

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPDATE_CLASS_LAYOUT:
                    if (mangeProductClassifyAdapter == null || classList.size() == 0)
                        return;
                    mangeProductClassifyAdapter.setData(classList);
                    mangeProductClassifyAdapter.notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }
    };

    private BaseCall<WaresManage.ListProductClassBeanReq> mProductClassCallback = new BaseCall<WaresManage.ListProductClassBeanReq>() {
        @Override
        public void call(WaresManage.ListProductClassBeanReq resp) {
            // TODO Auto-generated method stub
            if (resp.isRequestSuccess()) {
                classList = resp.getProclassList();
                dimessWaitingDialog();
                handler.sendEmptyMessage(MSG_UPDATE_CLASS_LAYOUT);
            } else {
                Slog.e("Failed update mProductClassCallback!!!!!!!  " + resp.getMessage());
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_product_class_manage);
        waresManage = ReqFactory.buildInterface(this,
                WaresManage.class);
        initView();
        waresManage.getShopClassAsync(mProductClassCallback);
    }

    private void initView() {
        View title = findViewById(R.id.vg_title);
        mTitle = new TitleView(this, title);
        mTitle.setCenterText(R.string.ui_manage_string_classify, null);
        mTitle = new TitleView(getApplicationContext(), title);
        mTitle.setLeftIcon(R.drawable.btn_back, this);
        mTitle.setRightText(R.string.ui_manage_string_classify_sort, this);
        manageBtnClassAdd = findViewById(R.id.manage_btn_class_add);
        manageProductLv = findViewById(R.id.manage_product_lv);
        manageBtnClassAdd.setOnClickListener(this);
        mangeProductClassifyAdapter = new MangeProductClassifyAdapter(this);
//        classList.add("咖啡");
        mangeProductClassifyAdapter.setData(classList);
        manageProductLv.setAdapter(mangeProductClassifyAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case TitleView.ID_LEFT:
                finish();
                break;
            case TitleView.ID_RIGHT:
                break;
            case R.id.manage_btn_class_add:
                startActivity(new Intent(getApplicationContext(), ActivityManageProductAddClassify.class));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        showWaitingDialog((ContextState) null);
    }
}
